package com.yy.train.ticket.train.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.train.ticket.goout.LineSection;
import com.yy.train.ticket.into.page.LineSectionPage;
import com.yy.train.ticket.train.dao.TtLineSectionDao;
import com.yy.train.ticket.train.dao.TtLineStationDao;
import com.yy.train.ticket.train.entity.TtLineSectionEntity;
import com.yy.train.ticket.train.entity.TtLineStationEntity;
import com.yy.train.ticket.train.entity.TtStationEntity;
import com.yy.train.ticket.train.service.TtLineSectionService;
import com.yy.train.ticket.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


@Service("ttLineSectionService")
public class TtLineSectionServiceImpl extends ServiceImpl<TtLineSectionDao, TtLineSectionEntity> implements TtLineSectionService {
    @Resource
    TtLineSectionDao ttLineSectionDao;
    @Resource
    TtLineStationDao ttLineStationDao;
    private static final Lock lock = new ReentrantLock();//创建 lock 实例

    @Override
    public R saveLineSection(TtLineSectionEntity ttLineSection) {
        Long lineId=  ttLineSection.getLineId();
        try {


        lock.lock();
        Long aLong = ttLineSectionDao.selectCount(Wrappers.<TtLineSectionEntity>lambdaQuery().eq(TtLineSectionEntity::getLineId, lineId));
        if (aLong == 0L) {

       // List<TtLineStationEntity> list=this.ttLineStationDao.selectList(Wrappers.<TtLineStationEntity>lambdaQuery().eq(TtLineStationEntity::getLineId,lineId));
            List<TtStationEntity> list=this.ttLineStationDao.lineStation(lineId);

            List<TtLineSectionEntity> li=new ArrayList<>();
        for (int i = 0; i < list.size()-1; i++) {
            TtStationEntity ttLineStationEntity=list.get(i);
            for (int j = i+1; j < list.size(); j++) {
                TtStationEntity ttLineStationEntity1=list.get(j);

                 String startStation=ttLineStationEntity.getStationName();
                 String  endStation=ttLineStationEntity1.getStationName();


                TtLineSectionEntity ttLineSectionEntity=new TtLineSectionEntity();
                ttLineSectionEntity.setStartStationId(ttLineStationEntity.getStationId());
                ttLineSectionEntity.setEndStationId(ttLineStationEntity1.getStationId());
                ttLineSectionEntity.setLineSectionName(startStation+"--"+endStation);
                ttLineSectionEntity.setLineId(lineId);
                li.add(ttLineSectionEntity);
            }
        }
        for (TtLineSectionEntity ls: li) {
            this.ttLineSectionDao.insert(ls);
        }

        return R.ok();
        }else {
            return R.error("已经生成路段");
        }
        }finally {
            lock.unlock();
        }
    }

    @Override
    public R sectionPage(LineSectionPage lineSectionPage) {

        Page<LineSectionPage> page = new Page<>(lineSectionPage.getPageNum(), lineSectionPage.getPageSize());

        IPage<LineSection> list = ttLineSectionDao.lineSectionPage(page, lineSectionPage);
        return R.ok(list);
    }
}